﻿using System;
using Dal;
using Policlinica;
using System.Data;
using System.Data.SqlClient;
using NLog;

namespace ADONetDal
{
    public class ADONetRepositoryDosage : ADONetRepositoryBase<Dosage>
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();

        protected override SqlCommand GetInsertCommand(Dosage entity)
        {
            var cmd = new SqlCommand("DosageInsert", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Count", entity.Count));
            cmd.Parameters.Add(new SqlParameter("DrugId", entity.Drug.Id));
            cmd.Parameters.Add(new SqlParameter("RecipeId", entity.Recipe.Id));
            logger.Trace("SqlCommand для записи данных Дозировки в базу сформирован");
            return cmd;
        }

        protected override SqlCommand GetDeleteCommand(int id)
        {
            var cmd = new SqlCommand("DosageDelete", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для логического удаления Дозировки id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetUpdateCommand(Dosage entity)
        {
            var cmd = new SqlCommand("DosageUpdate", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", entity.Id));
            cmd.Parameters.Add(new SqlParameter("Count", entity.Count));
            cmd.Parameters.Add(new SqlParameter("DrugId", entity.Drug.Id));
            cmd.Parameters.Add(new SqlParameter("RecipeId", entity.Recipe.Id));
            logger.Trace("SqlCommand для обновления данных Дозировки id = {0} в базе сформирован", entity.Id);
            return cmd;
        }

        protected override SqlCommand GetSelectCommand(int id)
        {
            var cmd = new SqlCommand("GetDosage", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Id", id));
            logger.Trace("SqlCommand для поиска Дозировки id = {0} сформирован", id);
            return cmd;
        }

        protected override SqlCommand GetSelectAllCommand()
        {
            throw new NotImplementedException();
        }

        protected override SqlCommand GetSelectByParameterCommand(object parameter, string columnName)
        {
            var cmd = new SqlCommand("GetDosageByParameter", GetConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("Parameter", parameter));
            cmd.Parameters.Add(new SqlParameter("Column", columnName));
            logger.Trace("SqlCommand для поиска Дозировки {0} = {1} сформирован", columnName, parameter);
            return cmd;
        }

        protected override Dosage GetEntity(SqlDataReader sqlDataReader)
        {
            Dosage dosage = new Dosage();
            IRepository<Drug> adoNetRepositoryDrug = new ADONetRepositoryDrug();
            IRepository<Recipe> adoNetRepositoryRecipe = new ADONetRepositoryRecipe();
            dosage.Id = (int) sqlDataReader["Id"];
            dosage.Count = sqlDataReader["Count"].ToString();
            dosage.Drug = adoNetRepositoryDrug.GetById((int)sqlDataReader["DrugId"]);
            dosage.Recipe = adoNetRepositoryRecipe.GetById((int)sqlDataReader["RecipeId"]);
            return dosage;
        }
    }
}
